﻿using System;
using System.Collections;
using System.IO;
using Character;
using UnityEngine;
using UnityEngine.UI;

// Token: 0x020001DD RID: 477
public class DataCustomEdit : MonoBehaviour
{
	// Token: 0x06000CB6 RID: 3254 RVA: 0x0004C5F0 File Offset: 0x0004A7F0
	public void Setup(Human human, EditEquipShow equipShow)
	{
		this.human = human;
		this.equipShow = equipShow;
		this.caraList.Seup(CardFileList.TYPE.CHARA, human.sex);
		this.equipList.Seup(CardFileList.TYPE.COORDINATE, human.sex);
		this.list_Back = Directory.GetFiles("UserData/CardFrame/Back", "*.png", SearchOption.AllDirectories);
		Array.Sort<string>(this.list_Back);
		this.list_Front = Directory.GetFiles("UserData/CardFrame/Front", "*.png", SearchOption.AllDirectories);
		Array.Sort<string>(this.list_Front);
		foreach (GameObject gameObject in this.tabs)
		{
			gameObject.SetActive(false);
		}
		this.SetCard_Back(this.backCard);
		this.SetCard_Front(this.frontCard);
	}

	// Token: 0x06000CB7 RID: 3255 RVA: 0x0004C6B0 File Offset: 0x0004A8B0
	private void OnEnable()
	{
		if (this.equipShow != null)
		{
			this.equipShow.SetAuto(EditEquipShow.WEARSHOW.ALL);
		}
	}

	// Token: 0x06000CB8 RID: 3256 RVA: 0x0004C6D0 File Offset: 0x0004A8D0
	private void OnDestroy()
	{
		if (this.captureTex != null)
		{
			UnityEngine.Object.Destroy(this.captureTex);
			this.captureTex = null;
		}
	}

	// Token: 0x06000CB9 RID: 3257 RVA: 0x0004C6F8 File Offset: 0x0004A8F8
	private void Update()
	{
	}

	// Token: 0x06000CBA RID: 3258 RVA: 0x0004C6FC File Offset: 0x0004A8FC
	public void ChangeToggles(int no)
	{
		if (no == 0)
		{
			this.ChangeTab(DataCustomEdit.TAB.CHARA);
		}
		else if (no == 1)
		{
			this.ChangeTab(DataCustomEdit.TAB.WEAR);
		}
		else
		{
			this.ChangeTab(DataCustomEdit.TAB.NONE);
		}
	}

	// Token: 0x06000CBB RID: 3259 RVA: 0x0004C72C File Offset: 0x0004A92C
	public void ChangeTab(DataCustomEdit.TAB tab)
	{
		SystemSE.Play(SystemSE.SE.CHOICE);
		this.nowTab = tab;
		for (int i = 0; i < 3; i++)
		{
			this.tabs[i].SetActive(this.nowTab == (DataCustomEdit.TAB)i);
		}
		this.cardRoot.SetActive(this.tabs[1].activeInHierarchy);
	}

	// Token: 0x06000CBC RID: 3260 RVA: 0x0004C788 File Offset: 0x0004A988
	public void Chara_Save(string file)
	{
		if (file == string.Empty)
		{
			this.charaOverwriteFile = string.Empty;
			this.ChangeTab(DataCustomEdit.TAB.CHARA_CAPTURE);
		}
		else
		{
			GameControl gameControl = UnityEngine.Object.FindObjectOfType<GameControl>();
			string[] choices = new string[]
			{
				"カードを撮影して上書き",
				"カードをそのままに上書き",
				"キャンセル"
			};
			Action[] array = new Action[3];
			array[0] = delegate()
			{
				this.Chara_SaveOverwriteCapture(file);
			};
			array[1] = delegate()
			{
				this.CharaOverwriteSaveExe(file);
			};
			Action[] acts = array;
			gameControl.CreateModalChoices("上書きしますか？", choices, acts);
		}
	}

	// Token: 0x06000CBD RID: 3261 RVA: 0x0004C82C File Offset: 0x0004AA2C
	public void Chara_Load(string file, int filter)
	{
		LOAD_MSG load_MSG = this.human.Load(file, filter);
		this.editMode.LoadedHuman();
		GameControl gameControl = UnityEngine.Object.FindObjectOfType<GameControl>();
		LOAD_MSG load_MSG2 = (LOAD_MSG)12;
		if ((load_MSG & LOAD_MSG.ISOMERISM) != LOAD_MSG.PERFECT)
		{
			gameControl.CreateModalMessageUI("異性のデータでした。\nカードファイルを入れるフォルダをご確認ください", null, false);
		}
		else if ((load_MSG & load_MSG2) != LOAD_MSG.PERFECT && ConfigData.anotherGameCardMessage)
		{
			string str = ((load_MSG & LOAD_MSG.VER_SEXYBEACH) == LOAD_MSG.PERFECT) ? "蜂蜜精选" : "セクシービーチ";
			string text = str + "で作成されたデータをロードしました。\n";
			text += "ゲーム毎の表現方法の違いにより、色の設定等が一部引き継げない場合や\n";
			text += "同じ設定でも見た目が異なってしまう場合があります。\n";
			text += "大変お手数ですが、再度設定をお願い致します。\n";
			gameControl.CreateModalMessageUI(text, new Action<bool>(this.LoadMsgOK_Version), true);
		}
	}

	// Token: 0x06000CBE RID: 3262 RVA: 0x0004C8EC File Offset: 0x0004AAEC
	private void LoadMsgOK_Version(bool doNotAgain)
	{
		if (doNotAgain)
		{
			ConfigData.anotherGameCardMessage = false;
		}
	}

	// Token: 0x06000CBF RID: 3263 RVA: 0x0004C8FC File Offset: 0x0004AAFC
	public void Coordinate_Save(string file)
	{
		if (file == string.Empty)
		{
			this.CoordinateSaveName();
		}
		else
		{
			GameControl gameControl = UnityEngine.Object.FindObjectOfType<GameControl>();
			gameControl.CreateModalYesNoUI("上書きしますか？", delegate
			{
				this.CoordinateSaveExe(file);
			}, null);
		}
	}

	// Token: 0x06000CC0 RID: 3264 RVA: 0x0004C95C File Offset: 0x0004AB5C
	public void Coordinate_Load(string file, int filter)
	{
		this.human.LoadCoordinate(file, filter);
		this.editMode.LoadedCoordinate();
	}

	// Token: 0x06000CC1 RID: 3265 RVA: 0x0004C978 File Offset: 0x0004AB78
	private void SetCard_Back(int no)
	{
		string file = string.Empty;
		string text = "カードなし";
		if (this.list_Back.Length > 0)
		{
			this.backCard = (this.list_Back.Length + no) % this.list_Back.Length;
			file = this.list_Back[this.backCard];
			text = this.backCard.ToString() + "/" + this.list_Back.Length.ToString();
		}
		else
		{
			this.cardBack.enabled = false;
		}
		this.LoadImage(this.cardBack, file);
		this.backCardSelText.text = text;
	}

	// Token: 0x06000CC2 RID: 3266 RVA: 0x0004CA24 File Offset: 0x0004AC24
	public void MoveCard_Back(int move)
	{
		this.SetCard_Back(this.backCard + move);
	}

	// Token: 0x06000CC3 RID: 3267 RVA: 0x0004CA34 File Offset: 0x0004AC34
	public void SwitchCard_Back(bool flag)
	{
		this.cardBack.enabled = (this.list_Back.Length > 0 && flag);
	}

	// Token: 0x06000CC4 RID: 3268 RVA: 0x0004CA54 File Offset: 0x0004AC54
	private void SetCard_Front(int no)
	{
		string file = string.Empty;
		string text = "カードなし";
		if (this.list_Front.Length > 0)
		{
			this.frontCard = (this.list_Front.Length + no) % this.list_Front.Length;
			file = this.list_Front[this.frontCard];
			this.LoadImage(this.cardFront, file);
			text = this.frontCard.ToString() + "/" + this.list_Front.Length.ToString();
		}
		else
		{
			this.cardFront.enabled = false;
		}
		this.frontCardSelText.text = text;
	}

	// Token: 0x06000CC5 RID: 3269 RVA: 0x0004CB00 File Offset: 0x0004AD00
	public void MoveCard_Front(int move)
	{
		this.SetCard_Front(this.frontCard + move);
	}

	// Token: 0x06000CC6 RID: 3270 RVA: 0x0004CB10 File Offset: 0x0004AD10
	public void SwitchCard_Front(bool flag)
	{
		this.cardFront.enabled = (this.list_Front.Length > 0 && flag);
	}

	// Token: 0x06000CC7 RID: 3271 RVA: 0x0004CB30 File Offset: 0x0004AD30
	private static Texture2D LoadPNG(string file)
	{
		byte[] array = null;
		FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
		if (fileStream == null)
		{
			return null;
		}
		using (BinaryReader binaryReader = new BinaryReader(fileStream))
		{
			try
			{
				array = binaryReader.ReadBytes((int)binaryReader.BaseStream.Length);
			}
			catch (Exception message)
			{
				GlobalData.Debug_LogWarning(message);
				array = null;
			}
			binaryReader.Close();
		}
		if (array == null)
		{
			return null;
		}
		Texture2D texture2D = new Texture2D(1, 1, TextureFormat.ARGB32, false);
		texture2D.LoadImage(array);
		array = null;
		return texture2D;
	}

	// Token: 0x06000CC8 RID: 3272 RVA: 0x0004CBD4 File Offset: 0x0004ADD4
	private void LoadImage(Image image, string file)
	{
		Texture2D texture2D = DataCustomEdit.LoadPNG(file);
		Vector2 vector = new Vector2((float)texture2D.width, (float)texture2D.height);
		Rect rect = new Rect(Vector2.zero, vector);
		Vector2 pivot = vector * 0.5f;
		if (texture2D != null)
		{
			image.sprite = Sprite.Create(texture2D, rect, pivot, 100f, 0U, SpriteMeshType.FullRect);
		}
		else
		{
			image.sprite = null;
		}
	}

	// Token: 0x06000CC9 RID: 3273 RVA: 0x0004CC44 File Offset: 0x0004AE44
	public void CharaCaptureButton()
	{
		SystemSE.Play(SystemSE.SE.YES);
		base.StartCoroutine(this.ReadPixEndFrame());
	}

	// Token: 0x06000CCA RID: 3274 RVA: 0x0004CC5C File Offset: 0x0004AE5C
	private IEnumerator ReadPixEndFrame()
	{
		this.editMode.moveableRoot.gameObject.SetActive(false);
		ShortCut_Help shortcutHelp = UnityEngine.Object.FindObjectOfType<ShortCut_Help>();
		if (shortcutHelp)
		{
			shortcutHelp.gameObject.SetActive(false);
		}
		yield return new WaitForEndOfFrame();
		int saveWidth = 252;
		int saveHeight = 352;
		float aspect = (float)saveWidth / (float)saveHeight;
		float w = (float)Screen.height * aspect;
		Texture2D tex2D = new Texture2D((int)w, Screen.height, TextureFormat.RGB24, false);
		tex2D.filterMode = FilterMode.Point;
		tex2D.wrapMode = TextureWrapMode.Clamp;
		tex2D.ReadPixels(new Rect
		{
			x = (float)Screen.width * 0.5f - w * 0.5f,
			y = 0f,
			width = w,
			height = (float)Screen.height
		}, 0, 0, false);
		tex2D.Apply();
		TextureScale.Bilinear(tex2D, saveWidth, saveHeight);
		this.editMode.moveableRoot.gameObject.SetActive(true);
		if (shortcutHelp)
		{
			shortcutHelp.gameObject.SetActive(true);
		}
		this.captureTex = tex2D;
		this.CharaSaveName();
		yield break;
	}

	// Token: 0x06000CCB RID: 3275 RVA: 0x0004CC78 File Offset: 0x0004AE78
	private void CharaSaveName()
	{
		if (this.charaOverwriteFile == string.Empty)
		{
			this.saveUI.Save_New(this.captureTex, new Action<string>(this.CharaSaveFile), new Action(this.CharaSaveCancel));
		}
		else
		{
			this.saveUI.Save_Overwrite(this.captureTex, delegate
			{
				this.CharaSaveExe(this.charaOverwriteFile);
			}, new Action(this.CharaSaveCancel));
		}
	}

	// Token: 0x06000CCC RID: 3276 RVA: 0x0004CCF4 File Offset: 0x0004AEF4
	private void CharaSaveCancel()
	{
		if (this.captureTex != null)
		{
			UnityEngine.Object.Destroy(this.captureTex);
			this.captureTex = null;
		}
	}

	// Token: 0x06000CCD RID: 3277 RVA: 0x0004CD1C File Offset: 0x0004AF1C
	private void CharaSaveFile(string name)
	{
		string file = "UserData\\Chara\\";
		file += ((this.human.sex != SEX.FEMALE) ? "male\\" : "female\\");
		file = file + name + ".png";
		string text = Directory.GetCurrentDirectory() + file;
		if (text.Length >= 246)
		{
			GameControl gameControl = UnityEngine.Object.FindObjectOfType<GameControl>();
			gameControl.CreateModalMessageUI("名前が長すぎます", delegate(bool flag)
			{
				this.CharaSaveName();
			}, false);
		}
		else if (File.Exists(file))
		{
			GameControl gameControl2 = UnityEngine.Object.FindObjectOfType<GameControl>();
			gameControl2.CreateModalYesNoUI("同名ファイルがあります\n上書きしますか？", delegate
			{
				this.CharaSaveExe(file);
			}, new Action(this.CharaSaveName));
		}
		else
		{
			this.CharaSaveExe(file);
		}
		this.charaOverwriteFile = string.Empty;
	}

	// Token: 0x06000CCE RID: 3278 RVA: 0x0004CE24 File Offset: 0x0004B024
	private void CharaSaveExe(string path)
	{
		this.human.Save(path, this.captureTex);
		if (this.captureTex != null)
		{
			UnityEngine.Object.Destroy(this.captureTex);
			this.captureTex = null;
		}
		this.toggleChara.Value = false;
		this.caraList.Replacement(true);
	}

	// Token: 0x06000CCF RID: 3279 RVA: 0x0004CE80 File Offset: 0x0004B080
	private void CharaOverwriteSaveExe(string path)
	{
		Texture2D texture2D = DataCustomEdit.LoadPNG(path);
		this.human.Save(path, texture2D);
		if (texture2D != null)
		{
			UnityEngine.Object.Destroy(texture2D);
			this.captureTex = null;
		}
		this.toggleChara.Value = false;
		this.caraList.Replacement(true);
	}

	// Token: 0x06000CD0 RID: 3280 RVA: 0x0004CED4 File Offset: 0x0004B0D4
	private void Chara_SaveOverwriteCapture(string file)
	{
		this.charaOverwriteFile = file;
		this.ChangeTab(DataCustomEdit.TAB.CHARA_CAPTURE);
	}

	// Token: 0x06000CD1 RID: 3281 RVA: 0x0004CEE4 File Offset: 0x0004B0E4
	private void CoordinateSaveName()
	{
		GameControl gameControl = UnityEngine.Object.FindObjectOfType<GameControl>();
		gameControl.CreateModalInputStringUI(true, "保存するファイル名を入力してください", new Action<string>(this.CoordinateSaveFile), null, false, string.Empty);
	}

	// Token: 0x06000CD2 RID: 3282 RVA: 0x0004CF18 File Offset: 0x0004B118
	private void CoordinateSaveFile(string name)
	{
		string file = "UserData\\coordinate\\";
		file += ((this.human.sex != SEX.FEMALE) ? "male\\" : "female\\");
		file = file + name + ".png";
		string text = Directory.GetCurrentDirectory() + file;
		if (text.Length >= 246)
		{
			GameControl gameControl = UnityEngine.Object.FindObjectOfType<GameControl>();
			gameControl.CreateModalMessageUI("名前が長すぎます", delegate(bool flag)
			{
				this.CoordinateSaveName();
			}, false);
		}
		else if (File.Exists(file))
		{
			GameControl gameControl2 = UnityEngine.Object.FindObjectOfType<GameControl>();
			gameControl2.CreateModalYesNoUI("同名ファイルがあります\n上書きしますか？", delegate
			{
				this.CoordinateSaveExe(file);
			}, new Action(this.CoordinateSaveName));
		}
		else
		{
			this.CoordinateSaveExe(file);
		}
	}

	// Token: 0x06000CD3 RID: 3283 RVA: 0x0004D014 File Offset: 0x0004B214
	private void CoordinateSaveExe(string path)
	{
		this.coordinateCapture.Save(path);
		this.toggleWear.Value = false;
		this.equipList.Replacement(true);
	}

	// Token: 0x04000CE7 RID: 3303
	[SerializeField]
	private ToggleButton toggleChara;

	// Token: 0x04000CE8 RID: 3304
	[SerializeField]
	private ToggleButton toggleWear;

	// Token: 0x04000CE9 RID: 3305
	[SerializeField]
	private GameObject[] tabs = new GameObject[3];

	// Token: 0x04000CEA RID: 3306
	[SerializeField]
	private EditMode editMode;

	// Token: 0x04000CEB RID: 3307
	private EditEquipShow equipShow;

	// Token: 0x04000CEC RID: 3308
	private Human human;

	// Token: 0x04000CED RID: 3309
	private DataCustomEdit.TAB nowTab = DataCustomEdit.TAB.NONE;

	// Token: 0x04000CEE RID: 3310
	[SerializeField]
	private GameObject cardRoot;

	// Token: 0x04000CEF RID: 3311
	[SerializeField]
	private Image cardBack;

	// Token: 0x04000CF0 RID: 3312
	[SerializeField]
	private Image cardFront;

	// Token: 0x04000CF1 RID: 3313
	[SerializeField]
	private Text backCardSelText;

	// Token: 0x04000CF2 RID: 3314
	[SerializeField]
	private Text frontCardSelText;

	// Token: 0x04000CF3 RID: 3315
	[SerializeField]
	private CoordinateCapture coordinateCapture;

	// Token: 0x04000CF4 RID: 3316
	[SerializeField]
	private CardFileList caraList;

	// Token: 0x04000CF5 RID: 3317
	[SerializeField]
	private CardFileList equipList;

	// Token: 0x04000CF6 RID: 3318
	[SerializeField]
	private CharaSaveUI saveUI;

	// Token: 0x04000CF7 RID: 3319
	private string[] list_Back;

	// Token: 0x04000CF8 RID: 3320
	private string[] list_Front;

	// Token: 0x04000CF9 RID: 3321
	private int backCard;

	// Token: 0x04000CFA RID: 3322
	private int frontCard;

	// Token: 0x04000CFB RID: 3323
	private Texture2D captureTex;

	// Token: 0x04000CFC RID: 3324
	private string charaOverwriteFile;

	// Token: 0x020001DE RID: 478
	public enum TAB
	{
		// Token: 0x04000CFE RID: 3326
		NONE = -1,
		// Token: 0x04000CFF RID: 3327
		CHARA,
		// Token: 0x04000D00 RID: 3328
		CHARA_CAPTURE,
		// Token: 0x04000D01 RID: 3329
		WEAR,
		// Token: 0x04000D02 RID: 3330
		NUM
	}
}
